package edu.alanhome.portablelib;

import android.R;
import android.app.Activity;
import android.os.Bundle;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class search extends Activity {
    /** Called when the activity is first created. */
    Button btnSearch;
    Spinner spner1;
    Spinner spner2;
    Spinner spner3;
    Spinner spner4;
    ArrayAdapter<CharSequence> adpter1;
    ArrayAdapter<CharSequence> adpter2;
    ArrayAdapter<CharSequence> adpter3;
    ArrayAdapter<CharSequence> adpter4;
    EditText edittext;
    TextView login;
    String[] doctyp = new String[]{"ALL","01","02","11","12"};
    String[] strsearchtyp = new String[]{"title","author","keyword","isbn","asordno","coden","callno","publisher",
    		"series","tpinyin","apinyin"};
	String[] mathcflg = new String[]{"forward","full"};
    String[] Sort= new String[]{"CATA_DATE","M_TITLE","M_AUTHOR","M_CALL_NO","M_PUBLISHER","M_PUB_YEAR"};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(edu.alanhome.portablelib.R.layout.main);
        findViews();
        setListeners();
    }
	
	void findViews()
	{
		btnSearch = (Button)findViewById(edu.alanhome.portablelib.R.id.Button01);
		
		edittext  = (EditText)findViewById(edu.alanhome.portablelib.R.id.EditText01);
		
		login     = (TextView)findViewById(edu.alanhome.portablelib.R.id.TextView03);
		
		spner1    = (Spinner)findViewById(edu.alanhome.portablelib.R.id.Spinner01);
		spner2    = (Spinner)findViewById(edu.alanhome.portablelib.R.id.Spinner02);
		spner3    = (Spinner)findViewById(edu.alanhome.portablelib.R.id.Spinner03);
		spner4    = (Spinner)findViewById(edu.alanhome.portablelib.R.id.Spinner04);
				
		adpter1   = ArrayAdapter.createFromResource(this, edu.alanhome.portablelib.R.array.doctype,
				R.layout.simple_spinner_item);
		adpter1.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
		spner1.setAdapter(adpter1);
		adpter2   = ArrayAdapter.createFromResource(this, edu.alanhome.portablelib.R.array.strsearchtype,
				R.layout.simple_spinner_item);
		adpter2.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
		spner2.setAdapter(adpter2);
		adpter3   = ArrayAdapter.createFromResource(this, edu.alanhome.portablelib.R.array.matchflag,
				R.layout.simple_spinner_item);
		adpter3.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
		spner3.setAdapter(adpter3);
		adpter4   = ArrayAdapter.createFromResource(this, edu.alanhome.portablelib.R.array.sort,
				R.layout.simple_spinner_item);
		adpter4.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
		spner4.setAdapter(adpter4);
		
	}
	void setListeners()
	{
		login.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				LayoutInflater inflater = LayoutInflater.from(search.this);
				final View view = inflater.inflate(edu.alanhome.portablelib.R.layout.login, null);
				AlertDialog.Builder builder = new AlertDialog.Builder(search.this);
				builder.setTitle("请输入帐号信息")
				.setView(view)
				.setPositiveButton(edu.alanhome.portablelib.R.string.ok, 
						new DialogInterface.OnClickListener() {
					
							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								
							}
				})
				.setNegativeButton(edu.alanhome.portablelib.R.string.cancle,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								
							}
						}).show();
			
				
				
			}
		});
		btnSearch.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String val;
				Intent it = new Intent(search.this,searchResult.class);
				Bundle bundle = new Bundle();
				
				val = edittext.getText().toString();
				bundle.putString("search", val);
				if(!val.trim().equals(""))
				{
					Log.i("PLnotnull",val);
					
					int pos = spner1.getSelectedItemPosition();
					bundle.putString("doctype", doctyp[pos]);
					
					pos = spner2.getSelectedItemPosition();
					bundle.putString("strsearchtype", strsearchtyp[pos]);
					
					pos = spner3.getSelectedItemPosition();
					bundle.putString("matchflag", mathcflg[pos]);
					
					pos = spner4.getSelectedItemPosition();
					bundle.putString("sort", Sort[pos]);
				
					it.putExtras(bundle);
					startActivity(it);					
				}
				else{
					Log.i("PLnull","this is a null");
					new AlertDialog.Builder(search.this)
					.setTitle("Oops!")
					.setMessage("请输入搜索内容 @_@")
					.setPositiveButton(edu.alanhome.portablelib.R.string.ok,new DialogInterface.OnClickListener() {
												
						public void onClick(DialogInterface dialog, int which) {
													
						}
					}).show();
					
				}
			}
		});
	}
}